package service;

import dao.FoodDao;
import dao.impl.FoodDaoImpl;
import org.apache.commons.beanutils.BeanUtils;
import pojo.Food;
import pojo.FoodType;
import util.LogUtil;
import util.Result;
import util.StringDataCheckUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FoodService {

    private FoodDao foodDao = new FoodDaoImpl();

    /**
     * 按条件查询到的菜品列表
     * @param request
     * @param response
     */
    public void foodList(HttpServletRequest request, HttpServletResponse response){
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Food food = new Food();
            BeanUtils.populate(food, parameterMap);
            if (food.getFoodIsDeleted() == null && (food.getFoodIsDeleted() != 0 || food.getFoodIsDeleted() !=1)) {
                Result.response(1001, "参数异常！", response);
                return;
            }
            List<Food> foodListByParam = foodDao.findFoodListByParam(food);
            if (foodListByParam == null){
                Result.response( 1002, "查询失败！", response);
                return;
            }
            Result.response(0, "查询成功！", foodDao.countFindFoodListByParam(food), response, foodListByParam);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取所有菜品类型
     * @param request
     * @param response
     */
    public void getAllFoodType(HttpServletRequest request, HttpServletResponse response) {
        List<FoodType> allFoodType = foodDao.getAllFoodType();
        if (allFoodType == null) {
            Result.response(1001, "查询失败！", response);
            return;
        }
        Result.response(200, "查询成功！",response ,allFoodType);
    }

    /**
     * 删除所选的菜品
     * @param request
     * @param response
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) {
        String[] ids = request.getParameterValues("ids[]");
        if (ids == null){
            Result.response(1002,"参数不正确!", response);
        }
        Boolean res = false;
        // 遍历ids
        for (String id : ids) {
            res = foodDao.delete(id) > 0 ? true : false;
        }
        if (res) {
            Result.response(200, "删除成功!", response);
            return;
        }
        Result.response(1001, "删除失败!", response);
    }

    /**
     * 添加菜品
     * @param request
     * @param response
     */
    public void add(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Food food = new Food();
            BeanUtils.populate(food, parameterMap);
            food.setFoodCreateTime(new Timestamp(System.currentTimeMillis()));
            food.setFoodUpdateTime(new Timestamp(System.currentTimeMillis()));
            if (foodDao.add(food)>0) {
                Result.response(200, "添加成功！", response);
                return;
            }else {
                Result.response(1001, "添加失败！", response);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 编辑菜品
     * @param request
     * @param response
     */
    public void edit(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Food food = new Food();
            BeanUtils.populate(food, parameterMap);
            food.setFoodUpdateTime(new Timestamp(System.currentTimeMillis()));
            if (foodDao.edit(food)>0) {
                Result.response(200, "编辑成功！", response);
                return;
            }else {
                Result.response(1001, "编辑失败！", response);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }


    /**
     * 编辑页面
     * @param request
     * @param response
     */
    public void editFoodPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String foodId = request.getParameter("foodId");
        if (!StringDataCheckUtil.check(foodId)){
            return;
        }
        Food editFood = foodDao.findFoodById(foodId);
        request.setAttribute("editFood", editFood);
        request.getRequestDispatcher("/WEB-INF/pages/foodManage/foodManage/foodEdit.jsp").forward(request, response);
    }

    /**
     * 菜品图片页面
     * @param request
     * @param response
     */
    public void foodImgPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String foodId = request.getParameter("foodId");
        if (!StringDataCheckUtil.check(foodId)){
            return;
        }
        Food editFood = foodDao.findFoodById(foodId);
        request.setAttribute("editFood", editFood);
        request.getRequestDispatcher("/WEB-INF/pages/foodManage/foodManage/foodImg.jsp").forward(request, response);
    }

    /**
     * 真删
     * @param request
     * @param response
     */
    public void realDelete(HttpServletRequest request, HttpServletResponse response) {
        String[] ids = request.getParameterValues("ids[]");
        if (ids == null){
            Result.response(1002,"参数不正确!", response);
        }
        Boolean res = false;
        // 遍历ids
        for (String id : ids) {
            res = foodDao.realDelete(id) > 0 ? true : false;
        }
        if (res) {
            Result.response(200, "删除成功!", response);
            return;
        }
        Result.response(1001, "删除失败!", response);
    }

    /**
     * 恢复
     * @param request
     * @param response
     */
    public void restore(HttpServletRequest request, HttpServletResponse response) {
        String restoreId = request.getParameter("restoreId");
        Boolean res = false;
        res = foodDao.restore(restoreId) > 0 ? true : false;
        if (res) {
            Result.response(200, "恢复成功!", response);
            return;
        }
        Result.response(1001, "恢复失败!", response);
    }

    /**
     * 添加菜品类型
     * @param request
     * @param response
     */
    public void addType(HttpServletRequest request, HttpServletResponse response) {
        String foodTypeName = request.getParameter("foodTypeName");
        if (!StringDataCheckUtil.check(foodTypeName)) {
            Result.response(1001, "不能为空！", response);
            return;
        }
        if (foodDao.checkTypeName(foodTypeName) != null) {
            Result.response(1003, "该菜品类型已存在！",response);
            return;
        }
        if (foodDao.addType(foodTypeName)>0) {
            LogUtil.addLog("添加了菜品类型["+foodTypeName+"]", request);
            Result.response(200, "添加成功！", response);
            return;
        }
        LogUtil.addLog("添加菜品类型["+foodTypeName+"]失败", request);
        Result.response(1002, "添加失败!", response);
    }

    /**
     * 设置菜品类型
     * @param request
     * @param response
     */
    public void editFoodType(HttpServletRequest request, HttpServletResponse response) {
        List<String> foodTypeIds = new ArrayList<>();
        for (Map.Entry<String, String[]> stringEntry : request.getParameterMap().entrySet()) {
            if (!"m".equals(stringEntry.getKey())){
                foodTypeIds.add(stringEntry.getValue()[0]);
            }
        }
        foodDao.setStatusTo0();
        for (String foodTypeId : foodTypeIds) {
            if (foodDao.changeTypeStatus(foodTypeId)<=0) {
                Result.response(1001, "修改失败！", response);
                return;
            }
        }
        Result.response(200, "修改成功！", response);
    }

    /**
     * 删除菜品类型
     * @param request
     * @param response
     */
    public void deleteFoodType(HttpServletRequest request, HttpServletResponse response) {
        List<String> foodTypeIds = new ArrayList<>();
        for (Map.Entry<String, String[]> stringEntry : request.getParameterMap().entrySet()) {
            if (!"m".equals(stringEntry.getKey())){
                foodTypeIds.add(stringEntry.getValue()[0]);
            }
        }
        for (String foodTypeId : foodTypeIds) {
            if (foodDao.deleteFoodType(foodTypeId)<=0) {
                Result.response(1001, "删除失败！", response);
                return;
            }
        }
        Result.response(200, "删除成功！", response);
    }
}
